package com.zs.deepseek.controller;

import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/10/25 上午10:44
 */
@RestController
public class DeepSeekController {
    private final OpenAiChatModel chatModel;

    public DeepSeekController(OpenAiChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @RequestMapping("/ai/generateSql")
    public String generate(@RequestParam(value = "message", defaultValue = "hello") String message) {
        String data = "需求：" + message + "\n" +
                "表结构：\n" +
                "`order_info`表：`order_no`,`total_amount`,`create_time`\n" +
                "字段create_time是订单创建时间，数据存储格式YYYY-MM-DD HH:MM:SS\n" +
                "补充：生成的sql语句不要中文注释\n" +
                "sql语句中：订单月份数据别名order_date，订单数量别名order_count";
        String response = this.chatModel.call(data);
        String sql = parseString(response);
        System.out.println("sql : " + sql);
        return sql;
    }


    private String parseString(String str) {
        int index = str.indexOf("sql");
        int index1 = str.indexOf(";", index);
        return str.substring(index + 3, index1).replaceAll("\n", " ");
    }
}
